﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class CMS_Control_SetRole : System.Web.UI.UserControl
{



    Usas.BLL.Cms.UsasDB udb = new Usas.BLL.Cms.UsasDB();

    Usas.Common.Cms.Role rol = new Usas.Common.Cms.Role();
    Usas.BLL.Cms.cms_purview_setting stg = new Usas.BLL.Cms.cms_purview_setting();
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public List<string> GetList()
    {
        try
        {
            List<string> gl = new List<string>();
            string[] hd = hidId.Value.TrimEnd(',').Split(',');
            for (int i = 0; i < hd.Length; i++)
            {
                gl.Add(hd[i]);
            }
            return gl;
        }
        catch
        {
            return null;
        }
    }

    public string Check()
    {
        if (hidId.Value.Trim().Length <= 0)
        {
            return "浏览权限不得为空！";
        }
        else
        {
            return "";
        }
    }

    public int SetValueTran(List<string> gl, List<string> gle, int id, Usas.Common.Cms.Role.RedType rt)
    {
        string[] nl = new string[gl.Count + gle.Count];
        List<string> list = new List<string>();
        System.Array.Copy(gl.ToArray(), 0, nl, 0, gl.Count);
        System.Array.Copy(gle.ToArray(), 0, nl, gl.Count, gle.Count);
       
        if (gl.IndexOf("D0") != -1 || gle.IndexOf("D0") != -1)
        {
            list.Add("D0");
        }
        else
        {
            list = ReMoveExist(nl);
        }
        return stg.DelAddP_Value(list, id, rol.GetTable(rt), 0);
    }

    private List<string> ReMoveExist(string[] pur)
    {
        List<string> list = new List<string>();
        for (int i = 0; i < pur.Length; i++)
        {
            if (pur[i] != null)
            {
                if (pur[i].Trim().Length > 0)
                {
                    if (list.IndexOf(pur[i].Replace('O', '0').Trim()) == -1)
                    {
                        list.Add(pur[i].Replace('O', '0').Trim());
                    }
                }
            }
        }

        return list;
    }

    public void SetValue(int id, Usas.Common.Cms.Role.RedType rt)
    {
        DataTable dtt = rol.GetRoleCurrent(id, rt, 0);
        string dpt = "";
        string ro = "";
        string emp = "";
        string pur = "";
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            string p_value = dtt.Rows[i]["p_value"].ToString().Trim().Replace('O', '0');
            if (p_value.IndexOf("D") != -1)
            {
                dpt += p_value.Replace("D", "") + ",";
            }
            else if (p_value.IndexOf("R") != -1)
            {
                ro += "'" + p_value + "',";
            }
            else
            {
                emp += p_value + ",";
            }
            pur += p_value + ",";
        }
        if (dpt.Length == 0)
        {
            dpt = "1=0";
        }
        else
        {
            dpt = " DepartmentId in (" + dpt.Substring(0, dpt.Length - 1) + ")";
        }

        if (ro.Length == 0)
        {
            ro = "1=0";
        }
        else
        {
            ro = " GROUPID in (" + ro.Substring(0, ro.Length - 1) + ")";
        }

        if (emp.Length == 0)
        {
            emp = "1=0";
        }
        else
        {
            emp = "EmployeeId in (" + emp.Substring(0, emp.Length - 1) + ")";
        }
        DataSet ds = udb.GetRoleDepEmpInfoByTag(dpt, ro, emp);
        string ename = "";
        string eid = "";
        if (ds != null)
        {
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                txtUserList.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
                ename += dt.Rows[i]["name"].ToString() + ",";
                eid += dt.Rows[i]["id"].ToString() + ",";
            }
            hidId.Value = eid;
        }
     
        choose.Attributes.Add("onclick", "window.open ('../Role/RoleMain.aspx?a=GetUserList&rid=" + Server.UrlEncode(eid) + "&ename=" + Server.UrlEncode(ename) + "', '权限选择', 'height=610px, width=630px, toolbar=no, menubar=no, scrollbars=no,status=no,top=300,left=300');return false;");

    }

    private DataSet GetEmpNameList(ArrayList arr, out string back)
    {
        string dpt = "";
        string ro = "";
        string emp = "";
        string pur = "";
        back = "";
        for (int i = 0; i < arr.Count; i++)
        {
            string p_value = arr[i].ToString().Trim().Replace('O', '0');
            if (p_value.IndexOf("D") != -1)
            {
                dpt += p_value.Replace("D", "") + ",";
            }
            else if (p_value.IndexOf("R") != -1)
            {
                ro += "'" + p_value + "',";
            }
            else
            {
                emp += p_value + ",";
            }
            pur += p_value + ",";
        }
        if (dpt.Length == 0)
        {
            dpt = "1=0";
        }
        else
        {
            dpt = " DepartmentId in (" + dpt.Substring(0, dpt.Length - 1) + ")";
        }

        if (ro.Length == 0)
        {
            ro = "1=0";
        }
        else
        {
            ro = " GROUPID in (" + ro.Substring(0, ro.Length - 1) + ")";
        }

        if (emp.Length == 0)
        {
            emp = "1=0";
        }
        else
        {
            emp = "EmployeeId in (" + emp.Substring(0, emp.Length - 1) + ")";
        }
        return udb.GetRoleDepEmpInfo(dpt, ro, emp);
    }

   
}
